package com.hqyj.lls.controller;

import com.hqyj.lls.entity.MyFile;
import com.hqyj.lls.service.MyFileService;
import com.hqyj.lls.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
public class FileController {

    @Autowired
    private MyFileService myFileService;

    //执行file的增删查改请求
    @RequestMapping("/myFileAjax/{url}")
    public ResultUtils myFileAjax(@PathVariable String url, MyFile myFile){
        switch (url){
            case "insert":
                return myFileService.insert(myFile);
            case "fileList":
                return myFileService.fileList();
        }
        return null;
    }

    //文档上传请求
    @RequestMapping("/file/upload")
    public ResultUtils upload(MultipartFile file, HttpServletRequest request) {
        return myFileService.upload(file,request);
    }

    //文件下载
    @RequestMapping("/file/downLoad")
    public ResponseEntity<byte[]> downLoad(String fileName,HttpServletRequest request) throws IOException{
        return myFileService.downLoad(fileName,request);
    }
}
